package cn.iocoder.yudao.module.erp.controller.admin.dp.dpsign;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.erp.controller.admin.dp.dpsign.vo.DpSignPageReqVO;
import cn.iocoder.yudao.module.erp.controller.admin.dp.dpsign.vo.DpSignRespVO;
import cn.iocoder.yudao.module.erp.controller.admin.dp.dpsign.vo.DpSignSaveReqVO;
import cn.iocoder.yudao.module.erp.dal.dataobject.dp.DpSignDO;
import cn.iocoder.yudao.module.erp.service.dp.DpSignService;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;
import javax.validation.*;
import javax.servlet.http.*;
import java.io.IOException;
import java.util.List;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;


@Tag(name = "管理后台 - 配送后程")
@RestController
@RequestMapping("/erp/dp-sign")
@Validated
public class DpSignController {

    @Resource
    private DpSignService dpSignService;

    @PostMapping("/create")
    @Operation(summary = "创建配送后程")
    // @PreAuthorize("@ss.hasPermission('erp:dp-sign:create')")
    public CommonResult<Long> createDpSign(@Valid @RequestBody DpSignSaveReqVO createReqVO) {
        return success(dpSignService.createDpSign(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新配送后程")
    // @PreAuthorize("@ss.hasPermission('erp:dp-sign:update')")
    public CommonResult<Boolean> updateDpSign(@Valid @RequestBody DpSignSaveReqVO updateReqVO) {
        dpSignService.updateDpSign(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除配送后程")
    @Parameter(name = "id", description = "编号", required = true)
    // @PreAuthorize("@ss.hasPermission('erp:dp-sign:delete')")
    public CommonResult<Boolean> deleteDpSign(@RequestParam("id") Long id) {
        dpSignService.deleteDpSign(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得配送后程")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    // @PreAuthorize("@ss.hasPermission('erp:dp-sign:query')")
    public CommonResult<DpSignRespVO> getDpSign(@RequestParam("id") Long id) {
        DpSignDO dpSign = dpSignService.getDpSign(id);
        return success(BeanUtils.toBean(dpSign, DpSignRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得配送后程分页")
    // @PreAuthorize("@ss.hasPermission('erp:dp-sign:query')")
    public CommonResult<PageResult<DpSignRespVO>> getDpSignPage(@Valid DpSignPageReqVO pageReqVO) {
        PageResult<DpSignDO> pageResult = dpSignService.getDpSignPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, DpSignRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出配送后程 Excel")
    // @PreAuthorize("@ss.hasPermission('erp:dp-sign:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportDpSignExcel(@Valid DpSignPageReqVO pageReqVO,
                                  HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<DpSignDO> list = dpSignService.getDpSignPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "配送后程.xls", "数据", DpSignRespVO.class,
                BeanUtils.toBean(list, DpSignRespVO.class));
    }

}
